﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.Model
Imports Exp.DATA_Service.Exp.DATA_Service
Imports Exp.DATA_Service

Public Class Season
    Inherits System.Web.UI.Page
    Const MENUNAME As String = "SEASON"
    Const MsgEditError As String = "Please, insert Season Code"
    Const MsgDeleteSuc As String = "Delete Data complete"

    Const DataExisting As String = "Data existing, please try again "
    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                Dim objUser As REM_User = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

            End If
            BindData()
        End If
    End Sub
#Region "Fucntion"
    Sub Save()
        Dim objREM_Season As New REM_Season
        If txtSeasonCode.Text <> "" Then
            If CheckIDExisting(txtSeasonCode.Text, 0) Then
                lblStatus.Text = "Data existing, please try again "
                Return
            Else
                lblStatus.Text = ""
            End If
            Dim Season_Service As New Season_Service
            objREM_Season.SeasonCode = txtSeasonCode.Text
            Season_Service.Add(objREM_Season)
            JavaScript.AlertMessage(Me.Page, SaveSuccess)

        Else

            JavaScript.AlertMessage(Me.Page, "Insert data, try again")
        End If
    End Sub
    Sub Edit()

        Dim objREM_Season As New REM_Season
        If txtSeasonCode.Text <> "" Then
            If CheckIDExisting(txtSeasonCode.Text, CInt(HDFieldSeasonID.Value.ToString)) Then
                'lblStatus.Text = "Data existing, please try again "
                JavaScript.AlertMessage(Me.Page, DataExisting)
                Return
            Else
                lblStatus.Text = ""
            End If
            Dim Season_Service As New Season_Service
            objREM_Season.Season_ID = HDFieldSeasonID.Value
            objREM_Season.SeasonCode = txtSeasonCode.Text
            Season_Service.Edit(objREM_Season)
            ' lblStatus.Text = "Edit Season complete"
            JavaScript.AlertMessage(Me.Page, SaveSuccess)
            HDFieldSeasonID.Value = ""
        Else
            'lblStatus.Text = "Insert data, try again"
            JavaScript.AlertMessage(Me.Page, "Insert data, try again")
        End If
    End Sub
    Sub BindData()
        Dim Season_Service As New Season_Service
        Dim objREM_Service As New List(Of REM_Season)
        objREM_Service = Season_Service.GetAll()
        gv_Data.DataSource = objREM_Service
        gv_Data.DataBind()
    End Sub

    Function CheckIDExisting(ByVal sName As String, ByVal Season_ID As Integer) As Boolean
        Dim Season_Service As New Season_Service
        Dim LobjREM_Season = Season_Service.GetByName(sName).FirstOrDefault

        If LobjREM_Season Is Nothing Then
            Return False
        Else

            If Season_ID = 0 Then Return True 'Case Add new 
            If LobjREM_Season.Season_ID <> Season_ID Then  Return True

        End If

        Return False
    End Function
#End Region
#Region "Event"

    Private Sub gv_data_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_data.RowCommand
        Dim commandArgs As String = e.CommandArgument.ToString()
        Dim REM_Season As New REM_Season
        Dim Season_Service As New Season_Service
        Dim iSeasonID As Integer
        iSeasonID = CType(commandArgs, Integer)
        If e.CommandName.ToLower = "edit" Then
            REM_Season = Season_Service.GetByID(iSeasonID)

            If Not REM_Season Is Nothing Then
                If REM_Season.SeasonCode <> "" Then
                    txtSeasonCode.Text = REM_Season.SeasonCode
                    HDFieldSeasonID.Value = iSeasonID
                    MultiView1.ActiveViewIndex = 1
                Else
                    txtSeasonCode.Text = ""
                End If
            End If

        ElseIf e.CommandName.ToLower = "delete" Then
            Season_Service.Delete(iSeasonID)
            txtSeasonCode.Text = ""
            JavaScript.AlertMessage(Me.Page, DeleteSuccess)
        End If
    End Sub

    Protected Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        If HDFieldSeasonID.Value = "" Then
            Save()
            BindData()
            txtSeasonCode.Text = ""

            MultiView1.ActiveViewIndex = 0
            JavaScript.AlertMessage(Me.Page, SaveSuccess)
        Else
            Edit()
            BindData()
            txtSeasonCode.Text = ""
            MultiView1.ActiveViewIndex = 0
            JavaScript.AlertMessage(Me.Page, SaveSuccess)
        End If
    End Sub

    Private Sub gv_data_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles gv_data.RowDeleting
        BindData()
    End Sub

    Private Sub gv_data_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gv_data.RowEditing
        BindData()
    End Sub
    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        HDFieldSeasonID.Value = ""
        txtSeasonCode.Text = ""
    End Sub
    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        HDFieldSeasonID.Value = ""
        txtSeasonCode.Text = ""
    End Sub
#End Region
End Class